<template>
    <Modal class="set-roles"
            title="设置职能数据权限"
            ok-text="保存"
            width="600"
            v-model="show"
            :mask-closable="false"
            :transfer="false">
        <div style="height: 600px;overflow-y: scroll">
            <div><span></span></div>
            <Tree :data="dutyPermissionTree" ref="tree" show-checkbox></Tree>

        </div>
        <div slot="footer">
            <Button v-show="hasButtonPermission('101010307')" @click="choose" type="primary">保存</Button>
        </div>
    </Modal>
</template>

<script>
    import * as dutyOpr from '../../http/duty.api'
    export default {
        name: "SetDutyPermission",
        data() {
            return {
                dutyPermissionTree: [],
            }
        },
        computed: {
            show: {  // 控制当前模块是否显示
                get() {
                    return this.$store.state.duty.showModal
                },
                set(value) {
                    this.$store.commit('duty/toggleShowModal', value)
                }
            }
        },
        watch: {
            show: function (newVal, oldVal) { // 控制当前模块是否显示
                if (this.show) {
                    this.getRolesList(); // 初始化组织架构树
                }
            },
        },
        mounted() {
        },
        created() {
            console.log('created');
        },
        methods: {
            hasButtonPermission(permission) {
                return this.$store.getters['login/hasButtonPermission'](permission);
            },
            choose() { // 模态框确定
                const _this  = this;
                const nodes = _this.$refs.tree.getCheckedNodes().filter(item => !item.origin.dataPermissionItems).map(item => item.origin);
                _this.$emit('dutyPermissionSelected', nodes);
                // 关闭模态框
                // _this.$Modal.remove()
                _this.$store.commit('duty/toggleShowModal', false)

            },
            getRolesList() { // 获取所有组织架构
                const _this = this;
                Promise.all([dutyOpr.datapermission(), dutyOpr.getDutyDetail(_this.$store.state.duty.selectedDuty.id)])
                .then(function (res) {
                    const resp = res.map(res => res.data.result)
                    console.log(resp)
                    function buildTree(node) {
                        let obj = {
                            title: node.name,
                            expand: true,
                            origin: node,
                            children: node.dataPermissionItems.map(item => ({title: item.name, origin: Object.assign(item, {type: node.code}), checked: resp[1].dataPermissions.find(it => it.permissionCode===item.code)}))
                        };
                        return obj;
                    }
                    _this.dutyPermissionTree = resp[0].dataPermissionTypes.map(type => buildTree(type));
                }).catch(function () {
                    console.log(arguments);
                });
            }
        }
     }
</script>

<style scoped>

</style>
